package com.bocai.freeman.view.activitys;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseActivity;
import com.bocai.freeman.utils.FileUtil;
import com.bocai.freeman.utils.ToolbarHelper;
import com.bumptech.glide.Glide;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by admin on 2016/5/20.
 * 个人信息
 */
public class PersonalActivity extends BaseActivity {

    @Bind(R.id.iv_icon)
    ImageView mIvIcon;
    @Bind(R.id.ll_nickname)
    LinearLayout mLlNickname;
    @Bind(R.id.ll_phone)
    LinearLayout mLlPhone;
    @Bind(R.id.ll_gender)
    LinearLayout mLlGender;
    @Bind(R.id.ll_age)
    LinearLayout mLlAge;
    @Bind(R.id.ll_name)
    LinearLayout mLlName;
    @Bind(R.id.ll_profession)
    LinearLayout mLlProfession;
    @Bind(R.id.ll_area)
    LinearLayout mLlArea;
    @Bind(R.id.rl_select_picture)
    RelativeLayout mRlSelectPicture;

    private AlertDialog mDialog;
    private View dialogView;
    private TextView mTvPhotograph,mTvFromGallery;

    private String photoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    @Override
    protected void initView() {
        super.initView();
        dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_select_picture_style, null);
        mTvPhotograph = (TextView) dialogView.findViewById(R.id.tv_photograph);
        mTvFromGallery = (TextView) dialogView.findViewById(R.id.tv_from_gallery);
        mDialog = new AlertDialog.Builder(this).setView(dialogView).create();
    }


    @Override
    protected void initEvent() {
        super.initEvent();
        ToolbarHelper.setup(this, R.string.personal, R.mipmap.left_return);

        click(mLlNickname).subscribe(o -> {
            Intent intent = new Intent(PersonalActivity.this, PersonalEditActivity.class);
            intent.putExtra("theme", getString(R.string.modify_nickname));
            startActivityForResult(intent, ITEM_THEME.NICKNAME.ordinal());
        });

        click(mLlName).subscribe(o -> {
            Intent intent = new Intent(PersonalActivity.this, PersonalEditActivity.class);
            intent.putExtra("theme", getString(R.string.modify_name));
            startActivityForResult(intent, ITEM_THEME.NAME.ordinal());
        });

        click(mRlSelectPicture).subscribe(o -> {
           mDialog.show();
        });

        click(mLlGender).subscribe(o1 -> {
            startActivityForResult(new Intent(PersonalActivity.this,EditGenderActivity.class),ITEM_THEME.GENDER.ordinal());
        });

        click(mLlAge).subscribe(o1 -> {
            startActivityForResult(new Intent(PersonalActivity.this,EditAgeActivity.class),ITEM_THEME.AGE.ordinal());
        });

        click(mTvPhotograph).subscribe(o -> {
            startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),ITEM_THEME.PHOTOGRAPH.ordinal());
        });
        click(mTvFromGallery).subscribe(o -> {
            // TODO: 2016/5/20 from photo gallery
//            PhotoPickerIntent intent = new PhotoPickerIntent(this);
//            intent.setPhotoCount(1);
//            intent.setShowCamera(false);
//            startActivityForResult(intent, ITEM_THEME.GALLERY.ordinal());
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
            startActivityForResult(intent,ITEM_THEME.GALLERY.ordinal());
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if(requestCode == ITEM_THEME.NAME.ordinal()){

            }else if(requestCode == ITEM_THEME.NICKNAME.ordinal()){

            }else if(requestCode == ITEM_THEME.GALLERY.ordinal()){
                mDialog.dismiss();
//                ArrayList<String> photos =
//                        data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
//                Log("photo");
//                BitmapFactory.Options options = new BitmapFactory.Options();
//                BitmapFactory.decodeFile(photos.get(0),options);
//                if(BitmapUtils.caculateInSampleSize(options,mIvIcon.getWidth(),mIvIcon.getHeight())>2){
////                    startPhotoZoom(Uri.parse(photos.get(0)));
//                }
                startPhotoZoom(data.getData());
            }else if(requestCode == ITEM_THEME.PHOTOGRAPH.ordinal()){
                mDialog.dismiss();
            }else if(requestCode == ITEM_THEME.PHOTOZOOM.ordinal()){
                Glide.with(PersonalActivity.this).load(photoPath).into(mIvIcon);
            }else if(requestCode == ITEM_THEME.GENDER.ordinal()){

            }
        }
    }

    private void startPhotoZoom(Uri uri) {
        int dp = 500;
        File parent = FileUtil.getInstance(
                getApplicationContext()).makeDir("head");
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);// 去黑边
        //intent.putExtra("scaleUpIfNeeded", true);// 去黑边
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);//输出是X方向的比例
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高，切忌不要再改动下列数字，会卡死
        intent.putExtra("outputX", dp);//输出X方向的像素
        intent.putExtra("outputY", dp);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
        intent.putExtra("noFaceDetection", true);
        photoPath = parent.getPath() +File.separator
                + System.currentTimeMillis() + ".png";
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(photoPath)));
        intent.putExtra("return-data", false); // 设置为不返回数据
        startActivityForResult(intent, ITEM_THEME.PHOTOZOOM.ordinal());
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    public enum ITEM_THEME{
        NICKNAME,
        NAME,
        GALLERY,
        PHOTOGRAPH,
        PHOTOZOOM,
        GENDER,
        AGE
    }
}
